package app.fugema.shop.util.passwardview;

import java.util.ArrayList;
import java.util.List;

import com.base.common.utils.LogUtil;
import com.jungly.gridpasswordview.GridPasswordView;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import app.fugema.shop.R;
//输入2次密码
public class PasswordAgainView extends RelativeLayout
{

    private View mView;
    private ImageView mClose;
    private TextView mTitle;
    private TextView mForgetPwd;
    private TextView mAgainPwdTips;
    private LinearLayout againPwdView;
    private GridPasswordView mPasswordFirst;
    private GridPasswordView mPassword;
    private PasswordKeyboardView mKeyboard;
    private List<String> passwordList;//记录键盘输入的值
    private StringBuilder mValue;//最后保存的密码

    //第一次密码记录键盘输入的值
    private List<String> firstPwdList;
    //第一次密码最后保存的密码
    private StringBuilder mFirstValue;

    public PasswordAgainView(Context context) {
        this(context, null);
    }

    public PasswordAgainView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PasswordAgainView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mView = View.inflate(context, R.layout.view_password, null);

        initView();
        initEvent();

        addView(mView);
    }

    private void initView()
    {
        mPassword = (GridPasswordView) mView.findViewById(R.id.view_password);
        mClose = (ImageView) mView.findViewById(R.id.img_close);
        mTitle = (TextView) mView.findViewById(R.id.tv_title);
        mForgetPwd = (TextView) mView.findViewById(R.id.tv_forgetPwd);
        mKeyboard = (PasswordKeyboardView) mView.findViewById(R.id.view_keyboard);
        /*
            //打乱数字的位置
            mKeyboard.shuffleKeyboard();
        */
        mForgetPwd.setVisibility(INVISIBLE);

        againPwdView = (LinearLayout) mView.findViewById(R.id.ll_again_pwd_view);
        mAgainPwdTips = (TextView) mView.findViewById(R.id.tv_again_pwd_tips);
        mPasswordFirst = (GridPasswordView) mView.findViewById(R.id.view_password_first);
        againPwdView.setVisibility(VISIBLE);
    }

    private void initEvent()
    {
        //第2次的密码
        mValue = new StringBuilder();
        passwordList = new ArrayList<>();
        //记录第一次密码
        mFirstValue = new StringBuilder();
        firstPwdList = new ArrayList<>();

        mKeyboard.setIOnKeyboardListener(new PasswordKeyboardView.IOnKeyboardListener() {

            @Override
            public void onInsertKeyEvent(String text) {

                    mValue.setLength(0);
                    mFirstValue.setLength(0);

                    if( firstPwdList.size() < 6 )
                    {
                        //先保存第一个密码
                        firstPwdList.add(text);
                    }
                    else
                    {
                        //在保存第2个密码
                        passwordList.add(text);
                    }

                    for (int i = 0; i < firstPwdList.size(); i++)
                    {
                        mFirstValue.append(firstPwdList.get(i));
                    }

                    for (int i = 0; i < passwordList.size(); i++)
                    {
                        mValue.append(passwordList.get(i));
                    }

                    //先设置 第一个密码 第2个密码
                    if( TextUtils.isEmpty(mPasswordFirst.getPassWord()) )
                    {
                        mPasswordFirst.setPassword( mFirstValue.toString() );
                    }
                    else if( mPasswordFirst.getPassWord().toString().length() == 6 )
                    {
                        mPassword.setPassword(mValue.toString());
                    }
                    else
                    {
                        mPasswordFirst.setPassword( mFirstValue.toString() );
                    }
                    LogUtil.e("pwd","FirstPWD = " +mPasswordFirst.getPassWord()+  "  PWD = " + mPassword.getPassWord() );
            }

            @Override
            public void onDeleteKeyEvent()
            {
                        //先设置 第一个密码 第2个密码
                        mValue.setLength(0);
                        mFirstValue.setLength(0);

                        //先删除第2个密码
                        if(passwordList.size() != 0)
                        {
                            passwordList.remove(passwordList.size()-1);
                            for (int i = 0; i < passwordList.size(); i++)
                            {
                                mValue.append(passwordList.get(i));
                            }

                            for (int i = 0; i < firstPwdList.size(); i++)
                            {
                                mFirstValue.append(firstPwdList.get(i));
                            }

                            mPassword.setPassword(mValue.toString());

                        }
                        else
                        {
                            //第2次密码删除完 开始删第1次 密码
                            if(firstPwdList.size() != 0)
                            {
                                firstPwdList.remove(firstPwdList.size()-1);
                            }
                            for (int i = 0; i < firstPwdList.size(); i++)
                            {
                                mFirstValue.append(firstPwdList.get(i));
                            }
                            mPasswordFirst.setPassword( mFirstValue.toString() );
                        }
                        LogUtil.e("pwd","FirstPWD = " +mPasswordFirst.getPassWord()+  "  PWD = " + mPassword.getPassWord() );

            }
        });
        //第一次密码
    }

    //获取输入的密码
    public String getPassword(){
        return mValue.toString();
    }

    //取消
    public ImageView getCloseImageView(){
        return mClose;
    }

    //标题
    public TextView getTitleTextView(){
        return mTitle;
    }

    //忘记密码
    public TextView getForgetTextView() {
        return mForgetPwd;
    }

    //输入密码控件
    public GridPasswordView getPswView()
    {
        return mPassword;
    }


    // 再次输入 的 提示
    public TextView getAgainPwdTipsTextView()
    {
        return mAgainPwdTips;
    }

    //第一次输入密码控件
    public GridPasswordView getFirstPwdView()
    {
        return mPasswordFirst;
    }

    public String getFirstPassword()
    {
        return mFirstValue.toString();
    }

}
